import type { MutableRefObject } from 'react';
import { useRef } from 'react';

export const isEqual = <T, U extends T>(a: T, b: U): boolean => {
  if (a === b) {
    return true;
  }
  if (typeof a !== typeof b) {
    return false;
  }
  const aKeys = Object.keys(a);
  const bKeys = Object.keys(b);
  if (aKeys.length !== bKeys.length) {
    return false;
  }

  return aKeys.every((i) => a[i] === b[i]);
};

export const useCustomRef = <T>(
  value: T,
): [() => T, (newValue: T) => void, MutableRefObject<T>] => {
  const ref = useRef(value);
  const getRef = () => ref.current;
  const setRef = (newValue: T) => (ref.current = newValue);
  return [getRef, setRef, ref];
};
